package org.eminem.cloud.gatewayzuulaggregation.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.eminem.cloud.gatewayzuulaggregation.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import rx.Observable;

@Service
public class AggregationService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<User> getUserById(Long id) {
        //创建一个被观察者
        return Observable.create(observer -> {
            //请求用户微服务的/user/{id}端点
            User user = this.restTemplate.getForObject("http://register-eureaka-user/"+id,User.class);
            observer.onNext(user);
            observer.onCompleted();
        });

    }


    @HystrixCommand(fallbackMethod = "fallback")
    public Observable<User> getMoiveUserById(Long id) {
        //创建一个被观察者
        return Observable.create(observer -> {
            //请求电影微服务的/user/{id}端点
            User movieUser = this.restTemplate.getForObject("http://register-eureaka-movie/"+id,User.class);
            observer.onNext(movieUser);
            observer.onCompleted();
        });
    }

    public User fallback(Long id){
       User user = new User();
       user.setId(-1L);
       return user;
    }
}
